package cn.easybuy.controller;

import cn.easybuy.pojo.*;
import cn.easybuy.service.order.OrderService;
import cn.easybuy.service.orderdetail.OrderDetailService;
import cn.easybuy.service.product.ProductService;
import cn.easybuy.service.productcategory.ProCategoryService;
import cn.easybuy.service.user.UserService;
import cn.easybuy.util.Constants;
import cn.easybuy.vo.SecurityUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {


    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProCategoryService proCategoryService;

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping("/Login/toLogin")
    public String toLogin(){
        return "pre/login";
    }

    /**
     * 跳转注册页面
     * @return
     */
    @RequestMapping("/Register/toRegister")
    public String toRegister(){
        return "pre/register";
    }


    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping("/Register")
    @ResponseBody
    public Object register(User user){
        //加密
        user.setPassword(SecurityUtil.md5Hex(user.getPassword()));
        Map<String,Object> map = new HashMap<>();
       if (userService.save(user)){
           map.put("status",1);
           map.put("message","登录成功");
       }else{
           map.put("status",0);
           map.put("message","注册失败,请稍后再尝试");
       }
        return map;
    }

    @RequestMapping("/Login")
    @ResponseBody
    public Object login(String loginName, String password, HttpSession session){
       User loginUser = userService.getOne(new QueryWrapper<User>().eq("loginName",loginName));
       Map<String,Object> map = new HashMap<>();
       if (loginUser==null) {
           map.put("status",0);
           map.put("message","用户名不存在");
       }else if(!loginUser.getPassword().equals(SecurityUtil.md5Hex(password))){
           map.put("status",0);
           map.put("message","用户名或密码错误!");
       }else{
           session.setAttribute(Constants.LOGIN_USER,loginUser);
           map.put("status",1);
       }
        return map;
    }

    /**
     * 注销
     * @return
     */
    @RequestMapping("/Login/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();//清空会话
        return "pre/login";
    }

    /**
     * 跳转我的订单页面
     * @param userId
     * @param currentPage
     * @param request
     * @return
     */
    @RequestMapping("/admin/order/index")
    public String orderIndex(Integer userId,@RequestParam(defaultValue = "1") Integer currentPage,
                             HttpServletRequest request){

        //分页
        PageHelper.startPage(currentPage, 2);
        //显示订单详情列表
        List<Order> orderList = orderService.list(new QueryWrapper<Order>().eq("userId",userId));
        for (Order order:orderList){
            List<OrderDetail> orderDetailList = orderDetailService.list(new QueryWrapper<OrderDetail>().eq("orderId",order.getId()));
            for (OrderDetail orderDetail :orderDetailList){
                orderDetail.setProduct(
                        productService.getById(orderDetail.getProductId())
                );
                order.setOrderDetailList(orderDetailList);
            }
        }
        //分页数据
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        Map<String,Object> pager = new HashMap<>();
        pager.put("currentPage",pageInfo.getPageNum());
        pager.put("pageCount",pageInfo.getPages());
        pager.put("url","admin/order/index?userId="+userId);
        request.setAttribute("pager",pager);
        request.setAttribute("orderList",orderList);

        return "backend/order/orderList";
    }

    /**
     * 跳转订单详情页面
     * @param orderId
     * @param request
     * @return
     */
    @RequestMapping("/admin/order/queryOrderDeatil")
    public String orderQueryOrderDetail(Integer orderId,HttpServletRequest request){

        //查询订单详情
        List<OrderDetail> orderDetailList = orderDetailService.list(new QueryWrapper<OrderDetail>().eq("orderId",orderId));
        for (OrderDetail orderDetail :orderDetailList){
            orderDetail.setProduct(
                    productService.getById(orderDetail.getProductId())
            );
        }
        request.setAttribute("orderDetailList",orderDetailList);
        return "backend/order/orderDetailList";
    }

    /**
     * 跳转到我的用户信息页面
     * @return
     */
    @RequestMapping("/admin/user/index")
    public String userIndex(){
        return "backend/user/userInfo";
    }

    /**
     * 跳转后台管理页面
     * @return
     */
    @RequestMapping("/admin/role/product/productList")
    public String productLists(@RequestParam(defaultValue = "1") Integer currentPage, HttpServletRequest request){
        //分页
        PageHelper.startPage(currentPage, 7);
        //商品列表
        List<Product> productList = productService.list();
        //分页数据
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        Map<String,Object> pager = new HashMap<>();
        pager.put("currentPage",pageInfo.getPageNum());
        pager.put("pageCount",pageInfo.getPages());
        pager.put("url","/admin/role/product/productList?");
        request.setAttribute("pager",pager);
        request.setAttribute("productList",productList);

        return "backend/product/productList";
    }

    /**
     * 跳转修改/删除商品列表页面
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/admin/role/product/toUpdateProduct")
    public String toUpdateProduct(Integer id,HttpServletRequest request){
        Product product = productService.getById(id);
        List<ProductCategory> productCategoryList1 = proCategoryService.list(new QueryWrapper<ProductCategory>().eq("parentId",0));
        List<ProductCategory> productCategoryList2 = proCategoryService.list(new QueryWrapper<ProductCategory>().eq("parentId",product.getCategoryLevel1Id()));
        List<ProductCategory> productCategoryList3 = proCategoryService.list(new QueryWrapper<ProductCategory>().eq("parentId",product.getCategoryLevel2Id()));

        request.setAttribute("product",product);
        request.setAttribute("productCategoryList1",productCategoryList1);
        request.setAttribute("productCategoryList2",productCategoryList2);
        request.setAttribute("productCategoryList3",productCategoryList3);
        return "backend/product/toAddProduct";
    }

    /**
     * 查询下级分类
     * @param parentId
     * @return
     */
    @RequestMapping("/admin/role/productCategory/queryProductCategoryList")
    @ResponseBody
    public Object queryProductCategoryList(Integer parentId){
        Map<String,Object> map = new HashMap<>();
        map.put("status",1);
        map.put("data", proCategoryService.list(new QueryWrapper<ProductCategory>().eq("parentId",parentId)));
        return  map;
    }

    /**
     * 修改商品列表
     * @param product
     * @param photo
     * @return
     */
    @RequestMapping("/admin/role/product/toAddProduct")
    public String toAddProduct(Product product,
                               @RequestParam("photo") MultipartFile photo){
        return "";
    }
}
